from PyQt5.QtWidgets import QWidget, QLabel, QVBoxLayout, QPushButton, QDesktopWidget, QLineEdit, QSplitter, QGridLayout
from PyQt5.QtCore import pyqtSignal, QThread
from PyQt5 import QtCore
from common import logger
from module.hdmiout.Tmain import test
from module.enumItem import Status
from module.enumItem import EventEnum
from module.enumItem import TaskID
from common import uuh_protocol


class UUHWindows(QWidget):
    finished = pyqtSignal(str)

    # INIT
    def __init__(self):
        super().__init__()
        self.screen = QDesktopWidget().screenGeometry()
        self.log = logger.Logger(__name__)
        self.test = test()
        self.test.test_signal.connect(self.slot_flush_text)
        self.test.finished.connect(self.slot_task_finish)
        
        self.test.init()
        self.initUI()
        
        self.dev_connect = False

    def initUI(self):
        self.setWindowTitle("Helper")

        # basic layout start
        self.main_layout = QVBoxLayout()
        self.layout_1 = QGridLayout()
        # basic layout end

        # layout 1 start
        self.lab_ip = QLabel("Ip Address:")
        self.lab_ip.setMaximumHeight(15)
        self.layout_1.addWidget(self.lab_ip, 0, 0)

        self.ip_line = QLineEdit("192.168.20.152")
        self.ip_line.setMinimumWidth(120)
        self.layout_1.addWidget(self.ip_line, 1, 0)

        self.btn_connect = QPushButton("Connect")
        self.btn_connect.clicked.connect(self.slot_btn_connect)
        self.layout_1.addWidget(self.btn_connect, 1, 1)

        self.main_layout.addLayout(self.layout_1)
        # layout 1 end

        splitter_1 = QSplitter()
        self.layout_1.addWidget(splitter_1)

        # set Main Layout
        self.setLayout(self.main_layout)
        self.setGeometry(100, 100, 300, 200)
        self.setWindowTitle('Helper')
        
    # ACTIVE
    def buttonClicked(self):
        # print (self.test.get_status())
        if self.test.get_status() == Status.STOP:
            self.test.set_status(Status.RUNNING)
            self.button.setText('Start')
        elif self.test.get_status() == Status.RUNNING:
            self.test.set_status(Status.STOP)
            self.button.setText('Stop')

    # ACTIVE
    def slot_btn_connect(self):
        self.btn_connect.setEnabled(False)
        if self.dev_connect:
            self.log.info('disconnect device')
            self.test.add_task(TaskID.DISCONNECT_DEV)

        else:
            self.log.info('connect device')
            self.test.add_task(TaskID.CONNECT_DEV)

    @QtCore.pyqtSlot(str)
    def slot_task_finish(self, ret):
        self.log.info('slot_task_finish function')
        if ret == 'unknow':
            self.log.error('unknow return string')
        elif ret == 'success':
            self.log.error
        self.btn_connect.setEnabled(True)


        
        

